<?php  if (!defined('BASEPATH')) exit('No direct script access allowed'); 

/**
 * Users_model provides access to the 'users' table in the database
 *
 * @author Casey McLaughlin
 * @package CIHeadStart
 * @link http://code.google.com/p/ciheadstart
 */
Class Users_model extends MY_Model
{
	function __construct()
	{
		parent::MY_Model();
		
		$this->load->library('encrypt');
	}
	
	// --------------------------------------------------------------------			
	
	function create($input_data, $return_id = TRUE, $hash_password = TRUE)
	{
		if (is_object($input_data))
			$input_data = (array) $input_data;
			
		if (isset($input_data['password']))
		{
			if ($hash_password)
				$input_data['password'] = $this->encrypt->sha1($input_data['password']);
			
			$input_data['password'] = $this->encrypt->encode($input_data['password']);
		}

		return parent::create($input_data, $return_id, $hash_password);
	}

	// --------------------------------------------------------------------			
	
	function update($user_identifier, $input_data, $hash_password = TRUE)
	{
		if (is_object($input_data))
			$input_data = (array) $input_data;
			
		if (isset($input_data['password']))
		{
			if ($hash_password)
				$input_data['password'] = $this->encrypt->sha1($input_data['password']);
				
			$input_data['password'] = $this->encrypt->encode($input_data['password']);
		}

		return parent::update($user_identifier, $input_data, $hash_password);
	}

	// --------------------------------------------------------------------			
	
	function retrieve_user($login_or_id, $return_pass = FALSE)
	{
		if ($user_record = $this->retrieve_single("`id` = '$login_or_id OR `login` = '$login_or_id'"))
		{
			if (isset($user_record->password))
			{
				if ($return_pass)
					$user_record->password = $this->encrypt->decode($user_record->password);
				else
					unset($user_record->password);
			}
			
			return $user_record;
		}
		else
			return $this->_do_error("Could not retrieve user record.");
	}
}

/* End of file users_model.php */
/* Location: ./application/models/users_model.php */